package vn.com.vega.chacha.v2.fragment;

import java.lang.ref.WeakReference;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.PlayerScreen;
import vn.com.vega.chacha.v2.player.PlayerService;
import vn.com.vega.music.clientserver.JsonSong;
import vn.com.vega.music.objects.Song;
import vn.com.vega.music.utils.Util;
import vn.com.vega.music.view.custom.LoadingView;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class PlayerViewLyricsFragment extends Fragment {
	private TextView mSongLyrics;
	private TextView mSongtitle;
	private LoadingView mLoadingView;
	private PlayerScreen mPlayerScreen;
	private String songLyric;
	private LoadLyric mLoadLyric;
	private Song mCurrentSong;
	private TextView mNoLyric;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.player_view_lyrics, null);
		mSongLyrics = (TextView) view.findViewById(R.id.tv_lyric_show);
		mSongtitle = (TextView) view.findViewById(R.id.tv_songtitle);
		mLoadingView = (LoadingView) view
				.findViewById(R.id.loadingView_view_lyric);
		mNoLyric = (TextView) view.findViewById(R.id.tv_song_no_lyric);
		mNoLyric.setVisibility(View.GONE);
		mLoadingView.setVisibility(View.GONE);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mPlayerScreen = (PlayerScreen) getActivity();
		mCurrentSong = null;
		// if (!Util.checkStringIsemptyOrNull(mPlayerScreen.songLyric)) {
		// mLyricShow.setText(Html.fromHtml(mPlayerScreen.songLyric));
		// songLyric = mLyricShow.getText().toString();
		// }
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mLoadLyric != null) {
			mLoadLyric.cancel(true);
		}
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// if (!Util.checkStringIsemptyOrNull(songLyric)) {
		// mLyricShow.setText(Html.fromHtml(songLyric));
		// } else {
		// if (mLoadLyric != null) {
		// mLoadLyric.cancel(true);
		// }
		// mCurrentSong = Util.getCurrentSongPlay();
		// if (mCurrentSong != null) {
		// mLoadLyric = new LoadLyric(this, mCurrentSong);
		// mLoadLyric.execute();
		// }
		// }
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(PlayerService.PLAYSTATE_CHANGED);
		mPlayerScreen.registerReceiver(receiver, intentFilter);
	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (PlayerService.PLAYSTATE_CHANGED.equalsIgnoreCase(action)) {
				Song currentSong = Util.getCurrentSongPlay();
				if (mCurrentSong == null || mCurrentSong.id != currentSong.id) {
					mCurrentSong = currentSong;
					mSongtitle.setText(mCurrentSong.title);
					songLyric = mPlayerScreen.getSongLyric(mCurrentSong);
					if (songLyric == null) {
						// Load song lyric
						if (mLoadLyric != null) {
							mLoadLyric.cancel(true);
						}
						mLoadLyric = new LoadLyric(
								PlayerViewLyricsFragment.this, mCurrentSong);
						mLoadLyric.execute();
					} else {
						showLyricForSong(mCurrentSong, songLyric);
					}

				}
			}
		}
	};

	private void showLyricForSong(Song song, String lyric) {
		if (Util.checkStringIsemptyOrNull(lyric)) {
			mSongLyrics.setVisibility(View.GONE);
			mNoLyric.setVisibility(View.VISIBLE);
		} else {
			mSongLyrics.setVisibility(View.VISIBLE);
			mNoLyric.setVisibility(View.GONE);
			mSongLyrics.setText(Html.fromHtml(lyric));
		}
		if (lyric != null) {
			mPlayerScreen.setLyricForSong(song, lyric);
		}
	}

	static class LoadLyric extends AsyncTask<Void, Void, Void> {
		WeakReference<PlayerViewLyricsFragment> lyricViewRfc;
		Song song;

		public LoadLyric(PlayerViewLyricsFragment fragment, Song song) {
			lyricViewRfc = new WeakReference<PlayerViewLyricsFragment>(fragment);
			this.song = song;
		}

		private boolean checkIsLive() {
			PlayerViewLyricsFragment fragment = lyricViewRfc.get();
			if (fragment == null) {
				return false;
			}
			if (fragment.mPlayerScreen == null
					|| fragment.mPlayerScreen.isDestroy) {
				return false;
			}
			return true;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if (!checkIsLive()) {
				return null;
			}
			PlayerViewLyricsFragment fragment = lyricViewRfc.get();
			fragment.songLyric = JsonSong.loadLyric(song);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (isCancelled() || !checkIsLive()) {
				return;
			}
			PlayerViewLyricsFragment fragment = lyricViewRfc.get();
			fragment.mLoadingView.loadingFinish(true, null);
			// if (Util.checkStringIsemptyOrNull(fragment.songLyric)) {
			// fragment.mLyricShow.setText(fragment.mPlayerScreen.getString(R.string.no_lyric));
			// } else {
			// fragment.mLyricShow.setText(Html.fromHtml(fragment.songLyric));
			// }
			fragment.showLyricForSong(song, fragment.songLyric);
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			if (!checkIsLive()) {
				return;
			}
			PlayerViewLyricsFragment fragment = lyricViewRfc.get();
			fragment.mLoadingView.startLoading();
		}

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		mPlayerScreen.unregisterReceiver(receiver);
	}

}
